package com.example.esaclient;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;
import android.util.Log;

import com.google.gson.Gson;


public class GetMyTags extends AsyncTask<Object, Integer, ArrayList<HashMap<String, Object>> > {
	
	private String URL ;
	private  static final String METHOD_NAME = "/GetMyTags?";	
	TabMentionTimeline caller;
	
	public GetMyTags(TabMentionTimeline callerActivity){
		caller=callerActivity;
	}
	
	@Override
	protected ArrayList<HashMap<String, Object>>  doInBackground(Object... params) {	
		
		GlobalSettings gs = new GlobalSettings(caller.getActivity());
		Boolean	LoggedToTwitter = gs.getPrefBool(GlobalSettings.PREF_KEY_TWITTER_LOGIN);
		Boolean	LoggedToFacebook = gs.getPrefBool(GlobalSettings.PREF_KEY_FACEBOOK_LOGIN);
		Boolean	LoggedToGoogle = gs.getPrefBool(GlobalSettings.PREF_KEY_GOOGLE_LOGIN);

		
		Log.w("ESA","Calling GetMyTags...");
			try{
				 
				 URL = "http://"+gs.getPrefString(GlobalSettings.PREF_ESA_URL)+"/ServiceRest.svc";			 
				 String url =URL+METHOD_NAME+"id="+params[0]+"&where="+params[1]+"&start_offset="+params[2]+"&end_offset="+params[3];
						  
				 HttpClient client = new DefaultHttpClient();
			        HttpGet httpGet = new HttpGet(url);
			        try {
			            HttpResponse response = client.execute(httpGet);
			            HttpEntity entity = response.getEntity();
			             
			            if(entity != null) {
			                InputStream stream = entity.getContent();
			                String responseString = GlobalSettings.convertStreamToString(stream);		                
			                
			                Wrapper w = new Gson().fromJson(responseString, Wrapper.class);
			                
			        		EsaTag et;
				    		ArrayList<HashMap<String, Object>> data=new ArrayList<HashMap<String,Object>>();
				    	
				    		for (int i = 0; i < w.GetMyTagsResult.length; i++) 
				    		{
				    			if (w.GetMyTagsResult[i] != null) {
				    				
				    				 if(isCancelled())   return data;
				    				 
				    				et = new EsaTag(w.GetMyTagsResult[i]);
				    				// data.add(ep.feed); }
				    				if (et.socialNetwork.equals("Twitter") && LoggedToTwitter )
				    					data.add(et.feed);
				    				else if (et.socialNetwork.equals("Facebook") && LoggedToFacebook)
				    					data.add(et.feed);
				    				else if (et.socialNetwork.equals("Google") && LoggedToGoogle )
				    					data.add(et.feed);
				    			}
				    		}
				    	 
			                
			                   return data;
			                   
			            }
			
			  } catch (ClientProtocolException e) {
		            e.printStackTrace();	            
		        } 
			        catch (IOException e) {
		            e.printStackTrace();
		           
		        }
		         
			}
		     catch (final Exception e) {
		         e.printStackTrace();   
		       
		     }
			return new ArrayList<HashMap<String, Object>> (); 
			}
		
		
		   public class Wrapper {
		    	 public String[] GetMyTagsResult;
		     }
	
	

	     protected void onPostExecute(ArrayList<HashMap<String, Object>> result) {
	    	 
	    
	    	 Log.w("ESA","Exiting GetMyTags...");
	    	 if(caller!=null)
	    	 caller.GetMyTagsResult(result);
	     }

	
	
}